package zy.data.food.controller;

import org.hibernate.validator.constraints.pl.REGON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zy.data.food.domain.ProductCategory;
import zy.data.food.domain.ProductInfo;
import zy.data.food.service.CategoryService;
import zy.data.food.service.ProductService;
import zy.data.food.util.ResultVOUtil;
import zy.data.food.vo.ProductInfoVO;
import zy.data.food.vo.ProductVO;
import zy.data.food.vo.ResultVO;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {

        @Autowired
        private ProductService productService;

        @Autowired
        private CategoryService categoryService;

        @GetMapping("/list")
        public ResultVO list(){
            //获取所有上架商品
            List<ProductInfo> productInfoList = productService.findAll();

            //查询所有类目
//            List<Integer> categoryTypeList = new ArrayList<>();

//            //传统写法
//            for(ProductInfo productInfo : productInfoList){
//                categoryTypeList.add(productInfo.getCategoryType());
//            }
            //新方法 (java8 lamda表达式)
            List<Integer> categoryTypeList = productInfoList.stream()
                    .map(e -> e.getCategoryType())
                    .collect(Collectors.toList());

            List<ProductCategory> productCategoryList =  categoryService.findByCategoryTypeIn(categoryTypeList);
            //数据拼装

            //第二层遍历类目
            List<ProductVO> productVOList = new ArrayList<>();
            for (ProductCategory productCategory : productCategoryList){

                    ProductVO productVO = new ProductVO();
                    productVO.setCategoryName(productCategory.getCategoryName());
                    productVO.setCategoryType(productCategory.getCategoryType());

                    //第三层产品遍历
                    List<ProductInfoVO> productInfoVOList = new ArrayList<>();
                    for(ProductInfo productInfo : productInfoList){
                        if(productInfo.getCategoryType() == productCategory.getCategoryType()){
                            System.out.println(productInfo.getCategoryType() + " " + productCategory.getCategoryType());
                            ProductInfoVO productInfoVO = new ProductInfoVO();
                            //BeanUtils将一个对象复制给另外一个对象
                            BeanUtils.copyProperties(productInfo,productInfoVO);
                            productInfoVOList.add(productInfoVO);
                        }
                    }
                    productVO.setProductInfoVOList(productInfoVOList);
                    productVOList.add(productVO);
            }
            //第一层

            return ResultVOUtil.success(productVOList);

        }


}
